package com.zb.manager.controller;

import com.zb.model.TAdmin;
import com.zb.model.TCategory;
import com.zb.model.TOrder;
import com.zb.model.TUser;
import com.zb.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 
 * @author jinzhenliang
 *
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {
	
	@Autowired
	private ManagerService ms;
	
	/**
	 * ����Ա��¼
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login")
	public String login()throws Exception{
		return "mlogin";
	}
	@RequestMapping("/ManagerIndex")
	public String ManagerIndex()throws Exception{
		return "manager_index";
	}
	
	/**
	 * ��֤�˺�����
	 * @param map
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/loginInspect")
	public String loginInspect(Map map,HttpServletRequest request,HttpSession session)throws Exception{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username+"====="+password);
		TAdmin admin=ms.selectAdmin(username,password);
		if(admin!=null) {
			map.put("userCount", ms.userSumCount());
			session.setAttribute("admin_name", username);
			session.setAttribute("admin", admin);
			return "manager_index";
		}
		
		return "mlogin";
	}
	
	/**
	 * ɾ��ָ���˻�
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/DelUserByUserId")
	public String delUserByUserId(HttpServletRequest request)throws Exception{
		String userId=request.getParameter("userId");
		ms.delUserByUserId(userId);
		return "admin/userlist";
	}

	/**
	 * ����Ա����û�
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addUser")
	public String addUser(HttpServletRequest request,HttpSession session,HttpServletResponse response)throws Exception{
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		TUser user=new TUser();
		user.setUsername(request.getParameter("username"));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//�������ڸ�ʽ
        String createtime = df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ��
        Date date=new Date();
		user.setCreateTime(date);
		user.setEmail(request.getParameter("email"));
		user.setPasswd(request.getParameter("password"));
		user.setPhone(request.getParameter("phone"));
		String uuid=UUID.randomUUID().toString();
		user.setUserId(uuid);
		ms.addUser(user);
		JOptionPane.showMessageDialog(null, "ADD OK");
		return "admin/userlist";
	}
	
	/**
	 * ����Ա�޸��û���Ϣ
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/reUser")
	public String reUser(HttpServletRequest request,HttpSession session,HttpServletResponse response)throws Exception{
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		TUser user=new TUser();
		String userid=request.getParameter("userid");
		user.setUserId(userid);
		user.setUsername(request.getParameter("username"));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//�������ڸ�ʽ
        String createtime = df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ��
        Date date=new Date();
		user.setUpdateTime(date);
		user.setEmail(request.getParameter("email"));
		user.setPasswd(request.getParameter("password"));
		user.setPhone(request.getParameter("phone"));
		ms.reUser(user);
		JOptionPane.showMessageDialog(null, "MOIDFY OK");
		return "admin/userlist";
	}
	
	/**
	 * ��ѯ�û��б�
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/UserList")
	public String findUserList(HttpSession session)throws Exception{
		System.out.println("-----------SELECT USER LIST");
		session.setAttribute("userCount", ms.userSumCount());
		session.setAttribute("userList", ms.userList());
		return "admin/userlist";
	}
	
	/**
	 * ģ����ѯ���û�����
	 * @param user_dimSearch
	 * @param session
	 * @return
	 */
	@RequestMapping("/dimUserList")
	public String DimUserList(String user_dimSearch,HttpSession session) {
		List<TUser> list=ms.dimSelectService(user_dimSearch);
		session.setAttribute("mdimuser", list);
		return"admin/dimuser";
	}
	
	/**
	 * ģ����ѯ����������
	 * @param cate_dimSearch
	 * @param session
	 * @return
	 */
	@RequestMapping("/dimCateList")
	public String DimCateList(String cate_dimSearch,HttpSession session) {
		List<TCategory> list=ms.dimCateSelectService(cate_dimSearch);
		session.setAttribute("mdimcate", list);
		return"admin/dimcate";
	}
	
	/**
	 * ģ����ѯ�������û�id��
	 * @param order_dimSearch
	 * @param session
	 * @return
	 */
	@RequestMapping("/dimOrderList")
	public String DimOrderList(String order_dimSearch,HttpSession session) {
		List<TOrder> list=ms.dimOrderSelectService(order_dimSearch);
		session.setAttribute("mdimorder", list);
		return"admin/dimorder";
	}
	
	/**
	 * �����б�
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/OrderList")
	public String findOrderList(HttpSession session)throws Exception{
		session.setAttribute("orderList", ms.orderList());
		return "admin/orderlist";
	}
	
	
	/**
	 * ɾ������
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteOrder")
	public String reOrder(HttpServletRequest request)throws Exception{
		String orderid=request.getParameter("orderid");
		ms.deleteOrder(orderid);
		return "admin/orderlist";
	}
	
	/**
	 * ��ȡ��������
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/getorders")
	public String loginInspect(HttpSession session,HttpServletRequest request) {
		List<TOrder> orderList=ms.findOrdersService();
		if(orderList!=null) {
			session.setAttribute("morderList", orderList);
			return "admin/orderlist";
		}else {
			return"admin/orderlist";
		}
			
	}
	
	
}
